"use strict";
cc._RF.push(module, '3ea00YI3l1IxZ8IsF3YGXfe', 'enemy');
// script/game/enemy.js

"use strict";

var State = {
  stand: 1,
  attack: 2,
  hurt: 3,
  die: 4
};
cc.Class({
  "extends": cc.Component,
  properties: {
    //最大血量
    maxHP: 5,
    //血条节点
    hpBar: {
      "default": null,
      type: cc.Node
    }
  },
  onLoad: function onLoad() {
    var _this = this;

    //当前血量
    this.hp = this.maxHP; //受击开关

    this.isHit = false; //攻击冷却时间

    this.actTimes = 0; //移动速度

    this.speedX = 100; //击倒速度

    this.hitDown = 80; //设置怪物坐标

    this.sp = cc.v2(0, 0);
    this.tt = 0;
    this.enemyState = State.stand; //移动方向开关

    this.moveLeft = false;
    this.moveRight = false;
    this.anim = 'idle';
    this.music = '';
    this.emAnim = this.node.getChildByName('body').getComponent(cc.Animation);
    this.rlv = this.node.getComponent(cc.RigidBody);
    this.bar = this.hpBar.getComponent(cc.ProgressBar);
    this.enemyMusic = this.node.getComponent(cc.AudioSource);
    this.rlv.fixedRotation = true; //固定初始旋转值

    this.rolesNode = cc.find("Canvas/role"); //    this.amountNode = cc.find("Canvas/bg/ShowNum/item01");
    // var actionBy = cc.jumpBy(2, cc.v2(145, -227), 50, 5);
    // this.node.runAction(actionBy)

    this.emAnim.on('finished', function (e, data) {
      //console.log(data.name);
      if (data.name == 'hurt') {
        _this.isHit = false;
        _this.enemyState = State.stand;

        _this.playMusic('punch2');

        _this.setAnim('idle');

        if (_this.hp == 0) {
          _this.death();
        } // console.log(this.hp);

      } else if (data.name == 'attack') {
        //this.isAttack = false;
        _this.setAnim('idle');

        _this.enemyState = State.stand;
      }

      if (_this.hp == 0 && data.name == 'die') {
        _this.hpBar.opacity = 0;

        _this.scheduleOnce(function () {
          // 这里的 this 指向 component
          this.node.destroy();
        }, 1.5);
      }
    });
  },
  // showKillAmo(string){
  //     this.numLable = this.amountNode.getComponent(cc.Label);
  //     this.numLable.string = string;
  // },
  // start () {
  // },
  playMusic: function playMusic(music) {
    this.enemyMusic.play(music);
  },
  setAnim: function setAnim(anim) {
    if (this.anim == anim) {
      return;
    }

    this.anim = anim;
    this.emAnim.play(anim);
  },
  attack: function attack() {
    // var attX = 200;
    // var attY = 100; 
    if (this.actTimes > 0) {
      //console.log(this.actTimes);
      return;
    } //   this.enemyState = State.attack;
    //this.isAttack = true;


    this.lv = this.rlv.linearVelocity;

    if (this.node.scaleX == 1) {
      var det = this.node.scaleX;
      this.lv.x = det * 100;
    } else if (this.node.scaleX == -1) {
      var det = this.node.scaleX;
      this.lv.x = det * 100;
    }

    this.actTimes = 150;
    this.lv.y = 150;
    this.setAnim('attack');
    this.rlv.linearVelocity = this.lv;
  },
  //被攻击状态
  isHurt: function isHurt() {
    if (this.isHit) {
      return;
    }

    this.isHit = true;
    this.enemyState = State.hurt;
    this.setAnim('hurt');

    if (this.hp > 0) {
      this.hp--;
      this.bar.progress = this.hp / this.maxHP; //  console.log(this.hp); 
    }

    this.lv = this.rlv.linearVelocity;
    this.lv.x = 100;
    this.rlv.linearVelocity = this.lv; //this.emAnim.play('hurt');
    // var self = this;
  },
  //死亡状态
  death: function death() {
    if (this.hp > 0) {
      return;
    }

    this.lv = this.rlv.linearVelocity;
    this.enemyState = State.die;

    if (this.node.scaleX == -1) {
      this.sp.x = 1;
      this.lv.x = this.sp.x * this.hitDown;
    } else if (this.node.scaleX == 1) {
      this.sp.x = -1;
      this.lv.x = this.sp.x * this.hitDown;
    }

    this.lv.y = 150;
    this.setAnim('die');
    this.rlv.linearVelocity = this.lv;
  },
  //移动状态
  move: function move() {
    var scaleX = Math.abs(this.node.scaleX);
    this.lv = this.rlv.linearVelocity;

    if (this.moveLeft) {
      this.sp.x = -1;
      this.node.scaleX = -scaleX;
      this.setAnim('run');
    } else if (this.moveRight) {
      this.sp.x = 1;
      this.node.scaleX = scaleX;
      this.setAnim('run');
    } else {
      this.sp.x = 0;
      this.setAnim('idle');
    }

    if (this.sp.x) {
      this.lv.x = this.sp.x * this.speedX;
    } else {
      this.lv.x = 0;
    }

    this.rlv.linearVelocity = this.lv;
  },
  //行为设置
  enemyAction: function enemyAction(tt) {
    var _this2 = this;

    var rl_pos = this.rolesNode.position;
    var en_pos = this.node.position;
    var dis = cc.Vec2.distance(en_pos, rl_pos);

    if (dis <= 110 && this.hp != 0) {
      // this.enemyState = State.stand;
      this.moveLeft = false;
      this.moveRight = false;

      if (this.enemyState != State.hurt) {
        setTimeout(function () {
          if (_this2.hp == 0) {
            return;
          }

          _this2.enemyState = State.attack;
        }, (tt + Math.random()) * 1500);
      } //this.enemyState = State.attack;

    } else if (dis <= 300) {
      //console.log("追击");
      var v = rl_pos.sub(en_pos); //    console.log(v.x);

      if (v.x < 0) {
        this.moveLeft = true;
        this.moveRight = false;
      } else {
        this.moveLeft = false;
        this.moveRight = true;
      }

      this.enemyState = State.stand;
    } else {
      //console.log("静止");
      this.moveLeft = false;
      this.moveRight = false; // this.setAnim('idle');

      this.enemyState = State.stand;
    }
  },
  update: function update(dt) {
    //console.log("dt",dt);
    //状态切换
    this.tt += dt;

    if (this.tt >= 2 && this.enemyState == State.stand) {
      this.enemyAction(dt);
      this.tt = 0;
    }

    if (this.enemyState == State.attack) {
      this.attack();

      if (this.actTimes > 0) {
        this.actTimes--;
      }
    } else if (this.enemyState == State.stand) {
      this.move();
    } //this.killNum;
    // if (this.hp == 0) {
    //     this.showKillAmo(this.num);
    // }

  }
});

cc._RF.pop();